﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace CoTuong
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TextScreen : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        SpriteFont myFont;


        Vector2 turnPosition;
        Vector2 debugPosition;
        int widthGame;
        int heightGame;

        public TextScreen(Game game)
            : base(game)
        {
            widthGame = game.Window.ClientBounds.Width;
            heightGame = game.Window.ClientBounds.Height;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            turnPosition.X = 600;
            turnPosition.Y = 10;
            debugPosition.X = 600;
            debugPosition.Y = 200;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            myFont = Game.Content.Load<SpriteFont>(@"Font\myFont");
            
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None);

            if (VanCo.isPlaying)
            {
                //Xuất lượt đi
                if (VanCo.LuotDi == 0)
                    spriteBatch.DrawString(myFont, "Luot Di: Do", turnPosition, Color.Red);
                else
                    spriteBatch.DrawString(myFont, "Luot Di: Xanh", turnPosition, Color.Green);

                //Xuất kết quả chiến thắng
                if (Game1.isEndGame == true)
                {
                    if (VanCo.LuotDi == 1)
                        spriteBatch.DrawString(myFont, "Winner: Do",
                            new Vector2(widthGame / 2 - 50,
                                        heightGame / 2),
                                        Color.Red, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                    else
                        spriteBatch.DrawString(myFont, "Winner: Xanh",
                            new Vector2(widthGame / 2 - 50,
                                        heightGame / 2),
                                        Color.Green, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

                    spriteBatch.DrawString(myFont, "Press Any Key To Continue",
                            new Vector2(widthGame / 2 - 120,
                                        heightGame / 2 + 80),
                                        Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                }
            }


            //Xuất danh sách người chơi đấu LAN
            if (Game1.isPlayingNetwork)
            {
                if (NetworkGame.networkSession != null && NetworkGame.listGamerTag.Count > 0)
                {

                    for (int i = 0; i < NetworkGame.networkSession.AllGamers.Count; i++)
                    {
                        Vector2 gamerTag_Position;
                        gamerTag_Position.X = Game.Window.ClientBounds.Width - myFont.MeasureString(NetworkGame.listGamerTag[i].ToString()).X;// - NetworkGame.profiles[i].GamerPicture.Width * 2;
                        if (i == 0)
                            gamerTag_Position.Y = Game.Window.ClientBounds.Height / 2;
                        else
                            gamerTag_Position.Y = Game.Window.ClientBounds.Height / 6;

                        if(i == 0)
                            spriteBatch.DrawString(myFont, NetworkGame.listGamerTag[i].ToString(), gamerTag_Position, Color.Red);
                        else
                            spriteBatch.DrawString(myFont, NetworkGame.listGamerTag[i].ToString(), gamerTag_Position, Color.Green);

                    }
                }
            }

            //Xuất thông báo lỗi
            if (!string.IsNullOrEmpty(Game1.errorMessage))
            {
                spriteBatch.DrawString(myFont, Game1.errorMessage, new Vector2(Game.Window.ClientBounds.Width / 2
                    - myFont.MeasureString(Game1.errorMessage).X, Game.Window.ClientBounds.Height / 2),
                    Color.Red);
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}